package com.salton123.charging.util;

import android.content.Context;
import android.media.AudioAttributes.Builder;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;

import com.salton123.charging.Consts;
import com.salton123.log.XLog;

public class MediaUtils {
    public static final String TAG = "MediaUtils";

    private static int convertStreamToUsage(int i) {
        if (i == 2) {
            return 6;
        }
        int i2 = 4;
        if (i != 4) {
            i2 = 5;
            if (i != 5) {
                return 1;
            }
        }
        return i2;
    }

    public static int[] saveAllStreamVolume(Context context) {
        try {
            return new int[]{getStreamVolume(context, 5), getStreamVolume(context, 2), getStreamVolume(context, 3),
                    getStreamVolume(context, 4)};
        } catch (Exception unused) {
            return new int[]{0, 0, 0, 0};
        }
    }

    public static void setMuteChargeSoundVolume(Context context, int i) {
        setStreamVolume(context, 5, i);
    }

    public static void restoreAllStreamVolume(Context context, int[] iArr) {
        try {
            setStreamVolume(context, 5, iArr[0]);
            setStreamVolume(context, 2, iArr[1]);
            setStreamVolume(context, 3, iArr[2]);
            setStreamVolume(context, 4, iArr[3]);
        } catch (Exception unused) {
        }
    }

    public static int getStreamVolume(Context context, int i) {
        try {
            return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamVolume(i);
        } catch (Exception unused) {
            return 0;
        }
    }

    public static int getMaxVolume(Context context, int i) {
        try {
            return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamMaxVolume(i);
        } catch (Exception unused) {
            return 0;
        }
    }

    public static void setStreamVolume(Context context, int i, int i2) {
        try {
            ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(i, i2, 0);
        } catch (Exception unused) {
        }
    }

    public static MediaPlayer playSound(final Context context, String str, OnCompletionListener onCompletionListener) {
        final int parseInt = Integer.parseInt(
                PrefsUtils.get().getString(Consts.KEY_SOUND_CHANNEL, Consts.DEFAULT_SOUND_CHANNEL_STR));
        final int streamVolume = getStreamVolume(context, parseInt);
        final int maxVolume = (int) (((float) (getMaxVolume(context, parseInt) *
                PrefsUtils.get().getInt(Consts.KEY_SOUND_STANDALONE_VOLUME, 0))) / 100.0f);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("【播放音频】初始音量=");
        stringBuilder.append(streamVolume);
        stringBuilder.append(" 独立设置的音量=");
        stringBuilder.append(maxVolume);
        stringBuilder.append(" 声音通道=");
        stringBuilder.append(parseInt);
        XLog.d(TAG, stringBuilder.toString());
        if (maxVolume != 0) {
            setStreamVolume(context, parseInt, maxVolume);
            stringBuilder = new StringBuilder();
            stringBuilder.append("【播放音频】设置后音量变成 ");
            stringBuilder.append(getStreamVolume(context, parseInt));
            XLog.d(TAG, stringBuilder.toString());
        }
        try {
            TrackingMediaPlayer trackingMediaPlayer = new TrackingMediaPlayer();
            StringBuilder stringBuilder2;
            if (str.startsWith("android.resource")) {
                Uri parse = Uri.parse(str);
                stringBuilder2 = new StringBuilder();
                stringBuilder2.append("【播放音频】: play with resId=");
                stringBuilder2.append(parse.toString());
                XLog.d(TAG, stringBuilder2.toString());
                trackingMediaPlayer.setDataSource(context, parse);
            } else {
                stringBuilder2 = new StringBuilder();
                stringBuilder2.append("【播放音频】: play with url=");
                stringBuilder2.append(str);
                XLog.d(TAG, stringBuilder2.toString());
                trackingMediaPlayer.setDataSource(str);
            }
            trackingMediaPlayer.setAudioAttributes(new Builder().setUsage(convertStreamToUsage(parseInt)).build());
            trackingMediaPlayer.setLooping(false);
            trackingMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
            trackingMediaPlayer.setOnStopListener(new TrackingMediaPlayer.OnStopListener() {
                public void onStop() {
                    if (maxVolume != 0) {
                        MediaUtils.setStreamVolume(context, parseInt, streamVolume);
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("【播放音频】onStop 将音量还原到");
                        stringBuilder.append(MediaUtils.getStreamVolume(context, parseInt));
                        XLog.d(TAG, stringBuilder.toString());
                    }
                }
            });
            trackingMediaPlayer.setOnErrorListener(new OnErrorListener() {
                public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
                    if (maxVolume != 0) {
                        MediaUtils.setStreamVolume(context, parseInt, streamVolume);
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("【播放音频】onError 将音量还原到");
                        stringBuilder.append(MediaUtils.getStreamVolume(context, parseInt));
                        XLog.d(TAG, stringBuilder.toString());
                    }
                    return false;
                }
            });
            final Context context2 = context;
            final OnCompletionListener onCompletionListener2 = onCompletionListener;
            trackingMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mediaPlayer) {
                    if (maxVolume != 0) {
                        MediaUtils.setStreamVolume(context2, parseInt, streamVolume);
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("【播放音频】onCompletion 将音量还原到");
                        stringBuilder.append(MediaUtils.getStreamVolume(context2, parseInt));
                        XLog.d(TAG, stringBuilder.toString());
                    }
                    OnCompletionListener onCompletionListener = onCompletionListener2;
                    if (onCompletionListener != null) {
                        onCompletionListener.onCompletion(mediaPlayer);
                    }
                }
            });
            trackingMediaPlayer.prepareAsync();
            return trackingMediaPlayer;
        } catch (Exception e) {
            e.printStackTrace();
            XLog.d(TAG, "【播放音频】出现异常：" + IOUtils.getExceptionStackInfo(e));
            return null;
        }
    }
}
